package com.moon;

import java.util.Calendar;
import java.util.Date;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.loopj.android.http.RequestParams;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.ToastUtil;
import com.util.Utils;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

@SuppressLint("ResourceAsColor")
public class CourseActivity extends BaseActivity {
	MyAdapter mAdapter;
	JSONArray mArrayList = null;
	int mWeekend;
	String mRemark = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_course);
		
		((TextView) findViewById(R.id.center)).setText(getIntent().getStringExtra("name") + "的课程表");
		findViewById(R.id.left).setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		
		final Calendar c = Calendar.getInstance();  
		mWeekend = c.get(Calendar.DAY_OF_WEEK) - 1;
		String date = Utils.ConverToString(new Date(), "yy-MM-dd") + " 第" + c.get(Calendar.WEEK_OF_YEAR) + "周";
		((TextView)findViewById(R.id.date)).setText(date);
		
		if(mWeekend > 0 && mWeekend < 6)
		{
			((TextView)findViewById(R.id.week0 + mWeekend - 1)).setTextColor(Color.parseColor("#7da700"));
		}
		
		final PullToRefreshListView ptrlvHeadlineNews = (PullToRefreshListView) findViewById(R.id.listview);
		ptrlvHeadlineNews.setMode(com.handmark.pulltorefresh.library.PullToRefreshBase.Mode.DISABLED);
		ListView listview = ptrlvHeadlineNews.getRefreshableView();
		listview.setVerticalScrollBarEnabled(false);
		listview.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});
		mAdapter = new MyAdapter();
		listview.setAdapter(mAdapter);
		
		sendRequest();
	}
	
	class MyAdapter extends BaseAdapter{
		public int getCount() {
			if(mArrayList == null) return 0;
			return mArrayList.size();
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			FoodHolder foodHold = null;

			if (convertView == null) {
				LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				foodHold = new FoodHolder();

				convertView = mInflater.inflate(R.layout.item_course, null);
				
				foodHold.index = (TextView)convertView.findViewById(R.id.index);
				foodHold.text0 = (TextView)convertView.findViewById(R.id.text0);
				foodHold.text1 = (TextView)convertView.findViewById(R.id.text1);
				foodHold.text2 = (TextView)convertView.findViewById(R.id.text2);
				foodHold.text3 = (TextView)convertView.findViewById(R.id.text3);
				foodHold.text4 = (TextView)convertView.findViewById(R.id.text4);
				foodHold.sep = (TextView)convertView.findViewById(R.id.sep);
				foodHold.start = (TextView)convertView.findViewById(R.id.start);
				foodHold.end = (TextView)convertView.findViewById(R.id.end);
				convertView.setTag(foodHold);
			} else {
				foodHold = (FoodHolder) convertView.getTag();
			}
			
			JSONArray item = mArrayList.getJSONArray(position);
			foodHold.index.setText(String.valueOf(position + 1));
			
			foodHold.text0.setText(item.getString(0));
			foodHold.text1.setText(item.getString(1));
			foodHold.text2.setText(item.getString(2));
			foodHold.text3.setText(item.getString(3));
			foodHold.text4.setText(item.getString(4));
			
			foodHold.text0.setBackgroundColor(Color.WHITE);
			foodHold.text1.setBackgroundColor(Color.WHITE);
			foodHold.text2.setBackgroundColor(Color.WHITE);
			foodHold.text3.setBackgroundColor(Color.WHITE);
			foodHold.text4.setBackgroundColor(Color.WHITE);
			
			foodHold.text0.setTextColor(R.color.text);
			foodHold.text1.setTextColor(R.color.text);
			foodHold.text2.setTextColor(R.color.text);
			foodHold.text3.setTextColor(R.color.text);
			foodHold.text4.setTextColor(R.color.text);
			
			if(mWeekend == 1)
			{
				foodHold.text0.setTextColor(Color.WHITE);
				foodHold.text0.setBackgroundColor(Color.parseColor("#a6d910"));
			}
			if(mWeekend == 2)
			{
				foodHold.text1.setTextColor(Color.WHITE);
				foodHold.text1.setBackgroundColor(Color.parseColor("#a6d910"));
			}
			if(mWeekend == 3)
			{
				foodHold.text2.setTextColor(Color.WHITE);
				foodHold.text2.setBackgroundColor(Color.parseColor("#a6d910"));
			}
			if(mWeekend == 4) 
			{
				foodHold.text3.setTextColor(Color.WHITE);
				foodHold.text3.setBackgroundColor(Color.parseColor("#a6d910"));
			}
			if(mWeekend == 5) 
			{
				foodHold.text4.setTextColor(Color.WHITE);
				foodHold.text4.setBackgroundColor(Color.parseColor("#a6d910"));
			}
			
			foodHold.sep.setVisibility(View.GONE);
			foodHold.start.setVisibility(View.INVISIBLE);
			foodHold.end.setVisibility(View.INVISIBLE);
			
			if(position == 4) 
			{
				foodHold.sep.setVisibility(View.VISIBLE);
				foodHold.sep.setText(mRemark);
			}
			if(position == 0) 
			{
				foodHold.start.setVisibility(View.VISIBLE);
//				foodHold.start.setText("00");
			}
			if(position == 7) 
			{
				foodHold.end.setVisibility(View.VISIBLE);
//				foodHold.end.setText("00");
			}
			
			return convertView;
		}

		public final class FoodHolder {
			public TextView sep;
			public TextView index;
			public TextView text0;
			public TextView text1;
			public TextView text2;
			public TextView text3;
			public TextView text4;

			public TextView start;
			public TextView end;
		}
	}
	
	private void sendRequest() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response).getJSONObject("data");
				mArrayList = ruser.getJSONArray("content");
				mRemark = ruser.getString("remark");
				mAdapter.notifyDataSetChanged();
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("class_id", 1);
		MgqRestClient.get("get_schedule_by_class_id", params, loginHandler);
	}
}
